package com.telnetnz.ticketdesk.common.config;

import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.rockbb.common.dao.CacheManagerBean;
import com.telnetnz.ticketdesk.dao.wrapper.AttachmentWrap;
import com.telnetnz.ticketdesk.dao.wrapper.ConfigWrap;
import com.telnetnz.ticketdesk.dao.wrapper.MemberWrap;
import com.telnetnz.ticketdesk.dao.wrapper.OperationLogWrap;
import com.telnetnz.ticketdesk.dao.wrapper.ProjectToUserWrap;
import com.telnetnz.ticketdesk.dao.wrapper.ProjectWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TagWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TemplateWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TicketCommentWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TicketToTagWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TicketToUserWrap;
import com.telnetnz.ticketdesk.dao.wrapper.TicketWrap;
import com.telnetnz.ticketdesk.dao.wrapper.UserWrap;

public class SpringContext
{
	private static Logger logger = Logger.getLogger(SpringContext.class);
	private static final SpringContext instance = new SpringContext();
	private ApplicationContext context;

	private SpringContext()
	{
		context = new ClassPathXmlApplicationContext(Constants.SPRING_CONF_PATH);
	}

	static public SpringContext getInstance()
	{
		return instance;
	}

	public ApplicationContext getContext()
	{
		return context;
	}

	public static freemarker.template.Configuration getFreeMarkerConfiguration()
	{
		return (freemarker.template.Configuration) instance.getContext().getBean("fmConfiguration");
	}

	public static SqlSessionFactoryBean getSqlSessionFactory()
	{
		return (SqlSessionFactoryBean) instance.getContext().getBean("sqlSessionFactory");
	}

	public static DefaultSqlSessionFactory getSqlSessionFactoryBean()
	{
		logger.debug("instance.getContext().getBean:::"
				+ instance.getContext().getBean("sqlSessionFactory").getClass().getName());
		return (DefaultSqlSessionFactory) instance.getContext().getBean("sqlSessionFactory");
	}

	public static void buildAll()
	{
		SpringContext.getSqlSessionFactoryBean().getConfiguration().buildAllStatements();
	}

	public static AttachmentWrap getAttachmentWrap()
	{
		return (AttachmentWrap) instance.getContext().getBean("attachmentWrap");
	}

	public static CacheManagerBean getCacheManagerBean() 
	{
		return (CacheManagerBean)instance.getContext().getBean("cacheManagerBean");
	}

	public static ConfigWrap getConfigWrap()
	{
		return (ConfigWrap) instance.getContext().getBean("configWrap");
	}

	public static MemberWrap getMemberWrap()
	{
		return (MemberWrap) instance.getContext().getBean("memberWrap");
	}

	public static OperationLogWrap getOperationLogWrap()
	{
		return (OperationLogWrap) instance.getContext().getBean("operationLogWrap");
	}

	public static ProjectWrap getProjectWrap()
	{
		return (ProjectWrap) instance.getContext().getBean("projectWrap");
	}

	public static ProjectToUserWrap getProjectToUserWrap()
	{
		return (ProjectToUserWrap) instance.getContext().getBean("projectToUserWrap");
	}

	public static TagWrap getTagWrap()
	{
		return (TagWrap) instance.getContext().getBean("tagWrap");
	}

	public static TemplateWrap getTemplateWrap()
	{
		return (TemplateWrap) instance.getContext().getBean("templateWrap");
	}

	public static TicketWrap getTicketWrap()
	{
		return (TicketWrap) instance.getContext().getBean("ticketWrap");
	}

	public static TicketCommentWrap getTicketCommentWrap()
	{
		return (TicketCommentWrap) instance.getContext().getBean("ticketCommentWrap");
	}

	public static TicketToTagWrap getTicketToTagWrap()
	{
		return (TicketToTagWrap) instance.getContext().getBean("ticketToTagWrap");
	}

	public static TicketToUserWrap getTicketToUserWrap()
	{
		return (TicketToUserWrap) instance.getContext().getBean("ticketToUserWrap");
	}

	public static UserWrap getUserWrap()
	{
		return (UserWrap) instance.getContext().getBean("userWrap");
	}
}
